package org.hk.kg.web.impl;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.hk.kg.web.BaseAction;
import org.hk.kg.web.HtmlRender;

public class ParamEleRender implements HtmlRender {
	private static final Pattern PARAM_PATTER = Pattern.compile("\\$\\{(\\w*)\\}");
	
	public boolean validate(String line) {
		return line.matches(".*\\$\\{\\w*\\}.*");
	}

	public String execute(String line, BaseAction context) {
		Matcher matcher = PARAM_PATTER.matcher(line);
		String tempLine = line;
		while(matcher.find())
		{
			String attr = matcher.group(1);
			Object valueTemp = context.getParams().get(attr);
			String value = valueTemp == null?"null":""+valueTemp;
			tempLine = tempLine.replace(matcher.group(),value);
		}
		return tempLine;
	}

}
